// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

var Take6Model = require("Take6Model");

cc.Class({
    extends: cc.Component,

    properties: {

    },

    // LIFE-CYCLE CALLBACKS:

    onLoad : function () 
    {
        var strDate = Take6Model.getRegisterDate();
        var nIconIndex = Take6Model.getPlayerIconIndex();

        if (strDate == "" || strDate == null || !strDate)
        {
            var myDate = new Date();
            strDate = myDate.getFullYear().toString() + "年" + myDate.getMonth().toString() + "月" + myDate.getDate().toString() + "日";
            Take6Model.setRegisterDate(strDate);
        }

        if (nIconIndex != 0 && !nIconIndex)
        {
            var myDate = new Date();
            var nIconIndex = myDate.getMilliseconds() % 15;
            Take6Model.setPlayerIconIndex(nIconIndex);
        }

        var dateLabel = this.node.getChildByName('dateLabel').getComponent(cc.Label);
        dateLabel.string = "注册日期:" + strDate;

        var strName = Take6Model.getPlayerName();
        if (strName != "" && strName != null)
        {
            var nameEdite = this.node.getChildByName('nameEditBox').getComponent(cc.EditBox);
            nameEdite.string = strName;
        }

        var iconBaseNode = this.node.getChildByName('iconBase');
        var iconNode = iconBaseNode.getChildByName('iconImage');
        iconNode.setScale(0.49);
        var iconScripte = iconNode.getComponent('IconImageScripte');
        iconScripte.setIcon(nIconIndex);

        var iconBaseNode = this.node.getChildByName('iconBase');

        var url = Take6Model.getPlayerIconURL() + "?aa=aa.jpg"; //"https://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTLicEOk3D36qntUBRo4yR9hicica2mBlhnibWE0rU798Drib5zvqyhyxTQ0bIBPWFOgM4LMtOL6VmAYWHQ/132?aa=aa.jpg";
        cc.loader.load(url, function (err, texture) {
         var frame = new cc.SpriteFrame(texture);
         // console.log("done");
         // cc.log(iconBaseNode);

         var webSpriteNode = iconBaseNode.getChildByName('WebSprite');
         var webSprite = webSpriteNode.getComponent(cc.Sprite);
         webSprite.spriteFrame = frame;
        });

        var roundLabel = this.node.getChildByName('RoundLabel').getComponent(cc.Label);
        roundLabel.string = Take6Model.getPlayerRoundSum();

        var scoreLabel = this.node.getChildByName('ScoreLabel').getComponent(cc.Label);
        scoreLabel.string = Take6Model.getPlayerScoreSum();
    },

    onOKButtonPressed : function ()
    {
        //todo 空白名字给提示
        var nameEdite = this.node.getChildByName('nameEditBox').getComponent(cc.EditBox)
        var strName = nameEdite.string;

        Take6Model.setPlayerName(strName);
        this.node.removeFromParent();
    }, 

    start () {

    },

    // update (dt) {},
});
